<?php

namespace System\Core;

use System\Lib\Log;
use \System\Types;

/**
 * Description of URI
 *
 * @author elton.schivei
 */
class URI extends Types\Object
{

    public $keyval = array();
    public $uri_string;
    public $segments = array();
    public $rsegments = array();

    /**
     * @var URI
     */
    private static $_instance;

    function __construct()
    {
        parent::__construct();
        $this->FetchUriString();
        Log::Write('URI Class Initialized', Log::DEBUG);
    }

    /**
     * Constructor
     *
     * Simply globalizes the $RTR object.  The front
     * loads the Router class early on so it's not available
     * normally as other classes are.
     */
    public static function &GetInstance()
    {
        if (!self::$_instance)
            self::$_instance = new self();

        return self::$_instance;
    }

    // --------------------------------------------------------------------

    /**
     * Get the URI String
     *
     * @access	private
     * @return	string
     */
    public function FetchUriString()
    {
        if (\strtoupper(Config::GetValue(Config::URI, 'uri_protocol')) == 'AUTO' || !Config::GetValue(Config::URI, 'uri_protocol'))
        {
            // Is the request coming from the command line?
            if (\defined('\STDIN'))
            {
                $this->SetUriString($this->_parse_cli_args());
                return;
            }

            // Let's try the REQUEST_URI first, this will work in most situations
            $uri = $this->_detect_uri();
            if ($uri)
            {
                $this->SetUriString($uri);
                return;
            }

            // Is there a PATH_INFO variable?
            // Note: some servers seem to have trouble with getenv() so we'll test it two ways
            $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @\getenv('PATH_INFO');
            if (\trim($path, '/') != '' && $path != "/" . SELF)
            {
                $this->SetUriString($path);
                return;
            }

            // No PATH_INFO?... What about QUERY_STRING?
            $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @\getenv('QUERY_STRING');
            if (\trim($path, '/') != '')
            {
                $this->SetUriString($path);
                return;
            }

            // As a last ditch effort lets try using the $_GET array
            if (\is_array($_GET) && \count($_GET) == 1 && \trim(\key($_GET), '/') != '')
            {
                $this->SetUriString(\key($_GET));
                return;
            }

            // We've exhausted all our options...
            $this->uri_string = '';
            return;
        }

        $uri = \strtoupper(Config::GetValue(Config::URI, 'uri_protocol'));

        if ($uri == 'REQUEST_URI')
        {
            $this->SetUriString($this->_detect_uri());
            return;
        }
        elseif ($uri == 'CLI')
        {
            $this->SetUriString($this->_parse_cli_args());
            return;
        }

        $path = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @\getenv($uri);
        $this->SetUriString($path);
    }

    // --------------------------------------------------------------------

    /**
     * Set the URI String
     *
     * @access	public
     * @return	string
     */
    public function SetUriString($str)
    {
        // Filter out control characters
        //if (class_exists("System\\Core\\Types\\String"))
        $str = Types\String::RemoveInvisibleCharacters($str, false)->ToString();

        // If the URI contains only a slash we'll kill it
        $this->uri_string = ($str == '/') ? '' : $str;
    }

    // --------------------------------------------------------------------

    /**
     * Detects the URI
     *
     * This function will detect the URI automatically and fix the query string
     * if necessary.
     *
     * @access	private
     * @return	string
     */
    private function _detect_uri()
    {
        if (!isset($_SERVER['REQUEST_URI']))
        {
            return '';
        }

        $uri = $_SERVER['REQUEST_URI'];
        if (\strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
        {
            $uri = \substr($uri, \strlen($_SERVER['SCRIPT_NAME']));
        }
        elseif (\strpos($uri, \dirname($_SERVER['SCRIPT_NAME'])) === 0)
        {
            $uri = \substr($uri, \strlen(\dirname($_SERVER['SCRIPT_NAME'])));
        }

        // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
        // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
        if (\strncmp($uri, '?/', 2) === 0)
        {
            $uri = \substr($uri, 2);
        }
        $parts = \preg_split('#\?#i', $uri, 2);
        $uri = $parts[0];
        if (isset($parts[1]))
        {
            $_SERVER['QUERY_STRING'] = $parts[1];
            \parse_str($_SERVER['QUERY_STRING'], $_GET);
        }
        else
        {
            $_SERVER['QUERY_STRING'] = '';
            $_GET = array();
        }

        if ($uri == '/' || empty($uri))
        {
            return '/';
        }

        $uri = \parse_url($uri, \PHP_URL_PATH);

        // Do some final cleaning of the URI and return it
        return \str_replace(array('//', '../'), '/', \trim($uri, '/'));
    }

    // --------------------------------------------------------------------

    /**
     * Parse cli arguments
     *
     * Take each command line argument and assume it is a URI segment.
     *
     * @access	private
     * @return	string
     */
    private function _parse_cli_args()
    {
        $args = \array_slice($_SERVER['argv'], 1);

        return $args ? '/' . \implode('/', $args) : '';
    }

    // --------------------------------------------------------------------

    /**
     * Filter segments for malicious characters
     *
     * @access	private
     * @param	string
     * @return	string
     */
    private function _filter_uri($str)
    {
        if ($str != '' && Config::GetValue(Config::SEGMENTS, 'permitted_uri_chars') != ''
                && Config::GetValue(Config::SEGMENTS, 'enable_query_strings') == false)
        {
            // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
            // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
            if (!\preg_match("|^[" .
                            \str_replace(array('\\-', '\-'), '-', \preg_quote(Config::GetValue(Config::SEGMENTS, 'permitted_uri_chars'), '-'))
                            . "]+$|i", $str))
            {
                Errors\Error::ShowErr('The URI you submitted has disallowed characters.', 400);
            }
        }

        // Convert programatic characters to entities
        $bad = array('$', '(', ')', '%28', '%29');
        $good = array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;');

        return \str_replace($bad, $good, $str);
    }

    // --------------------------------------------------------------------

    /**
     * Remove the suffix from the URL if needed
     *
     * @access	private
     * @return	void
     */
    public function RemoveUrlSuffix()
    {
        if (Config::GetValue(Config::SEGMENTS, 'url_suffix') != "")
        {
            $this->uri_string = \preg_replace("|" . \preg_quote(Config::GetValue(Config::SEGMENTS, 'url_suffix'))
                    . "$|", "", $this->uri_string);
        }
    }

    // --------------------------------------------------------------------

    /**
     * Explode the URI Segments. The individual segments will
     * be stored in the $this->segments array.
     *
     * @access	private
     * @return	void
     */
    public function ExplodeSegments()
    {
        foreach (\explode("/", \preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
        {
            // Filter segments for security
            $val = \trim($this->_filter_uri($val));

            if ($val != '')
            {
                $this->segments[] = $val;
            }
        }
    }

    // --------------------------------------------------------------------
    /**
     * Re-index Segments
     *
     * This function re-indexes the $this->segment array so that it
     * starts at 1 rather than 0.  Doing so makes it simpler to
     * use functions like $this->uri->segment(n) since there is
     * a 1:1 relationship between the segment array and the actual segments.
     *
     * @access	private
     * @return	void
     */
    public function ReindexSegments()
    {
        \array_unshift($this->segments, null);
        \array_unshift($this->rsegments, null);
        unset($this->segments[0]);
        unset($this->rsegments[0]);
    }

    // --------------------------------------------------------------------

    /**
     * Fetch a URI Segment
     *
     * This function returns the URI segment based on the number provided.
     *
     * @access	public
     * @param	integer
     * @param	bool
     * @return	string
     */
    public function Segment($n, $no_result = false)
    {
        return (!isset($this->segments[$n])) ? $no_result : $this->segments[$n];
    }

    // --------------------------------------------------------------------

    /**
     * Fetch a URI "routed" Segment
     *
     * This function returns the re-routed URI segment (assuming routing rules are used)
     * based on the number provided.  If there is no routing this function returns the
     * same result as $this->segment()
     *
     * @access	public
     * @param	integer
     * @param	bool
     * @return	string
     */
    public function RSegment($n, $no_result = false)
    {
        return (!isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
    }

    // --------------------------------------------------------------------

    /**
     * Generate a key value pair from the URI string
     *
     * This function generates and associative array of URI data starting
     * at the supplied segment. For example, if this is your URI:
     *
     * 	example.com/user/search/name/joe/location/UK/gender/male
     *
     * You can use this function to generate an array with this prototype:
     *
     * array (
     * 			name => joe
     * 			location => UK
     * 			gender => male
     * 		 )
     *
     * @access	public
     * @param	integer	the starting segment number
     * @param	array	an array of default values
     * @return	array
     */
    public function URIToAssoc($n = 3, $default = array())
    {
        return $this->_uri_to_assoc($n, $default, 'segment');
    }

    /**
     * Identical to above only it uses the re-routed segment array
     *
     */
    function RURIToAssoc($n = 3, $default = array())
    {
        return $this->_uri_to_assoc($n, $default, 'rsegment');
    }

    // --------------------------------------------------------------------

    /**
     * Generate a key value pair from the URI string or Re-routed URI string
     *
     * @access	private
     * @param	integer	the starting segment number
     * @param	array	an array of default values
     * @param	string	which array we should use
     * @return	array
     */
    private function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
    {
        if ($which == 'segment')
        {
            $total_segments = 'TotalSegments';
            $segment_array = 'SegmentArray';
        }
        else
        {
            $total_segments = 'TotalRSegments';
            $segment_array = 'RSegmentArray';
        }

        if (!\is_numeric($n))
        {
            return $default;
        }

        if (isset($this->keyval[$n]))
        {
            return $this->keyval[$n];
        }

        if ($this->$total_segments() < $n)
        {
            if (\count($default) == 0)
            {
                return array();
            }

            $retval = array();
            foreach ($default as $val)
            {
                $retval[$val] = false;
            }
            return $retval;
        }

        $segments = \array_slice($this->{$segment_array}(), ($n - 1));

        $i = 0;
        $lastval = '';
        $retval = array();
        foreach ($segments as $seg)
        {
            if ($i % 2)
            {
                $retval[$lastval] = $seg;
            }
            else
            {
                $retval[$seg] = false;
                $lastval = $seg;
            }

            $i++;
        }

        if (\count($default) > 0)
        {
            foreach ($default as $val)
            {
                if (!\array_key_exists($val, $retval))
                {
                    $retval[$val] = false;
                }
            }
        }

        // Cache the array for reuse
        $this->keyval[$n] = $retval;
        return $retval;
    }

    // --------------------------------------------------------------------

    /**
     * Generate a URI string from an associative array
     *
     *
     * @access	public
     * @param	array	an associative array of key/values
     * @return	array
     */
    public function AssocToUri($array)
    {
        $temp = array();
        foreach ((array) $array as $key => $val)
        {
            $temp[] = $key;
            $temp[] = $val;
        }

        return \implode('/', $temp);
    }

    // --------------------------------------------------------------------

    /**
     * Fetch a URI Segment and add a trailing slash
     *
     * @access	public
     * @param	integer
     * @param	string
     * @return	string
     */
    public function SlashSegment($n, $where = 'trailing')
    {
        return $this->_slash_segment($n, $where, 'segment');
    }

    // --------------------------------------------------------------------

    /**
     * Fetch a URI Segment and add a trailing slash
     *
     * @access	public
     * @param	integer
     * @param	string
     * @return	string
     */
    public function SlashRSegment($n, $where = 'trailing')
    {
        return $this->_slash_segment($n, $where, 'rsegment');
    }

    // --------------------------------------------------------------------

    /**
     * Fetch a URI Segment and add a trailing slash - helper function
     *
     * @access	private
     * @param	integer
     * @param	string
     * @param	string
     * @return	string
     */
    private function _slash_segment($n, $where = 'trailing', $which = 'segment')
    {
        $leading = '/';
        $trailing = '/';

        if ($where == 'trailing')
        {
            $leading = '';
        }
        elseif ($where == 'leading')
        {
            $trailing = '';
        }

        return $leading . $this->$which($n) . $trailing;
    }

    // --------------------------------------------------------------------

    /**
     * Segment Array
     *
     * @access	public
     * @return	array
     */
    public function SegmentArray()
    {
        return $this->segments;
    }

    // --------------------------------------------------------------------

    /**
     * Routed Segment Array
     *
     * @access	public
     * @return	array
     */
    public function RSegmentArray()
    {
        return $this->rsegments;
    }

    // --------------------------------------------------------------------

    /**
     * Total number of segments
     *
     * @access	public
     * @return	integer
     */
    public function TotalSegments()
    {
        return \count($this->segments);
    }

    // --------------------------------------------------------------------

    /**
     * Total number of routed segments
     *
     * @access	public
     * @return	integer
     */
    public function TotalRsegments()
    {
        return count($this->rsegments);
    }

    // --------------------------------------------------------------------

    /**
     * Fetch the entire URI string
     *
     * @access	public
     * @return	string
     */
    public function URIString()
    {
        return $this->uri_string;
    }

    // --------------------------------------------------------------------

    /**
     * Fetch the entire Re-routed URI string
     *
     * @access	public
     * @return	string
     */
    public function RURIString()
    {
        return '/' . \implode('/', $this->RSegmentArray());
    }

}
